// ErrorList1.cpp : implementation file
//

#include "stdafx.h"
#include "MoldingOHT.h"
#include "ErrorList.h"

#include "Util\MinUtilExt.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CErrorList dialog


CErrorList::CErrorList(CWnd* pParent /*=NULL*/)
	: CDialog(CErrorList::IDD, pParent)
{
	//{{AFX_DATA_INIT(CErrorList)
	//}}AFX_DATA_INIT
	m_nTotalItemCount = 0;
}


void CErrorList::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CErrorList)
	DDX_Control(pDX, IDB_FROM, m_bFrom);
	DDX_Control(pDX, IDB_TO, m_bTo);
	DDX_Control(pDX, IDB_LISTPGUP, m_bListPGUP);
	DDX_Control(pDX, IDB_LISTPGDN, m_bListPGDN);
	DDX_Control(pDX, IDB_EXIT, m_bExit);
	DDX_Control(pDX, IDB_SEARCH, m_bSearch);
	DDX_Control(pDX, IDC_DATETIME_SEARCH, m_ctrlSearch);
	DDX_Control(pDX, IDC_DATETIME_SEARCH2, m_ctrlSearch1);
	//}}AFX_DATA_MAP
	DDX_Control(pDX, IDC_ERRORCODE, m_ErrorListCtrl);
}

BEGIN_MESSAGE_MAP(CErrorList, CDialog)
	//{{AFX_MSG_MAP(CErrorList)
	ON_BN_CLICKED(IDB_LISTPGUP, OnListpgup)
	ON_BN_CLICKED(IDB_LISTPGDN, OnListpgdn)
	ON_BN_CLICKED(IDB_EXIT, OnExit)
	ON_BN_CLICKED(IDB_SEARCH, OnSearch)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CErrorList message handlers

void CErrorList::OnListpgup() 
{
	// TODO: Add your control notification handler code here
	m_ScrollSize.cx = 0;
	m_ScrollSize.cy = (m_nColumHeight * 30) * -1;

	m_ErrorListCtrl.Scroll(m_ScrollSize);
}
  
void CErrorList::OnListpgdn() 
{
	// TODO: Add your control notification handler code here
	m_ScrollSize.cx = 0;
	m_ScrollSize.cy = (m_nColumHeight * 30);

	m_ErrorListCtrl.Scroll(m_ScrollSize);
}

void CErrorList::OnExit() 
{
	// TODO: Add your control notification handler code here
	m_ErrorListCtrl.DeleteAllItems();

	CDialog::OnCancel();
}

BOOL CErrorList::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	if( pMsg->hwnd == GetDlgItem(IDC_ERRORCODE)->m_hWnd ) 
	{
		if( pMsg->message == WM_LBUTTONDOWN ) 
			 pMsg->message = 0;
	}
	
	return CDialog::PreTranslateMessage(pMsg);
}

BOOL CErrorList::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	SetWindowText("Error History");
	ButtonInitial();
	ListCtrlInit();

	m_ErrorListCtrl.SetExtendedStyle(LVS_EX_FLATSB | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_ONECLICKACTIVATE);
	m_strDirectoryName = _T(".\\data\\ErrorData\\");

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

BOOL CErrorList::ListCtrlInit()
{
	LV_COLUMN lvcolumn;
	//LV_ITEM lvitem;
 
	char* Title[2] = {"Error Time", "Error List"};
	int width[2] = {115,580};
	
	m_ErrorListCtrl.DeleteAllItems();
	
	for(int i=0;i<2;i++)
	{
		lvcolumn.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH;
		lvcolumn.fmt = LVCFMT_LEFT;
		lvcolumn.pszText = Title[i];
		lvcolumn.iSubItem = i;
		lvcolumn.cx = width[i];
		m_ErrorListCtrl.InsertColumn(i,&lvcolumn);
	}
	return TRUE;
}

void CErrorList::ButtonInitial()
{
	// Function Button
}


void CErrorList::OnSearch() 
{
	// TODO: Add your control notification handler code here
	m_ErrorListCtrl.DeleteAllItems();
	
	//	LV_COLUMN lvcolumn;
	LV_ITEM lvitem;
	int	 j, k, l;
	CTime		m_pStartTime;
	CTime		m_pEndTime;
	int	nStartYear, nStartMonth, nStartDay;
	int	nEndYear, nEndMonth, nEndDay;
	CString strFileName = _T("");
	
	m_ctrlSearch.GetTime(m_pStartTime);
	nStartYear = m_pStartTime.GetYear();
	nStartMonth = m_pStartTime.GetMonth();
	nStartDay = m_pStartTime.GetDay();
	
	m_ctrlSearch1.GetTime(m_pEndTime);
	nEndYear = m_pEndTime.GetYear();
	nEndMonth = m_pEndTime.GetMonth();
	nEndDay = m_pEndTime.GetDay();
	
	for(j=nStartYear; j<=nEndYear; j++)
	{
		for(k=nStartMonth; k<=nEndMonth; k++)
		{
			for(l=nStartDay; l<=nEndDay; l++)
			{
				//strFileName.Format(_T("%s%s%04d%s%02d%s%02d.txt"),m_strDirectoryName, "Error_",j,"-", k,"-", l);
				strFileName.Format(_T("%s%04d%02d.dat"),m_strDirectoryName, j, k);
				
				FILE* fp;
				
				if((fp = fopen(strFileName, "r")) == NULL)
				{	
					MyMessageBox("Error List File that does not read.","Read Error!", M_ICONERROR);	
					return;
				}
				
				lvitem.mask = LVIF_TEXT;
				
				char buf[1000];
				char buf_1[1000]; 
				char buf_2[1000];
				char tmp[1000];
				char* p;
				
				for(int i=0;i<10000;i++)
				{
					fgets( buf,1000,fp );
					if( feof(fp) ) break;
					
					lvitem.iItem = i;
					lvitem.iSubItem = 0;
					//	sprintf(tmp,"%d",i+1);
					lvitem.pszText = tmp;
					m_ErrorListCtrl.InsertItem(&lvitem);
					
					p = strtok( buf,"," );
					if(p != NULL) strcpy(buf_1, p);
					else strcpy(buf_1, "");
					
					p = strtok(NULL,"\n");
					if(p != NULL) strcpy(buf_2, p);
					else strcpy(buf_2, "");
					
					lvitem.iSubItem = 0;
					sprintf(tmp,"%s",buf_1);
					lvitem.pszText = tmp;
					m_ErrorListCtrl.SetItem(&lvitem);
					
					lvitem.iSubItem = 1;
					sprintf(tmp,"%s",buf_2);
					lvitem.pszText = tmp;
					m_ErrorListCtrl.SetItem(&lvitem);		
				}
				
				fclose(fp);
				
				CRect tmpRect;
				m_ErrorListCtrl.GetItemRect(0, &tmpRect, LVIR_LABEL);
				m_nColumHeight = tmpRect.bottom - tmpRect.top;
				
			}
		}
	}
}
